<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/12
 * Time: 11:31
 */

namespace app\index\controller;

use app\index\model\LoginModel;
use app\lib\exception\ErrorMessage;
use app\lib\exception\SuccessMessage;
use think\Controller;
use think\Session;

class Login extends Controller
{
    public function login()
    {
        if(request()->isPost()){
            $data['mobile'] = input('post.mobile');
            $data['password'] = md5(md5(input('post.password')));

            $userLogin = new LoginModel();
            $info = $userLogin->vailateUserInfo($data);

            if($info){
                session('id', $info['id']);         //用户ID
                session('username', $info['username']);  //用户名
                session('pic', $info['pic']); //用户头像
                session('code', $info['code']);    //推广码
                session('mobile', $info['mobile']);        //手机号

                return json(['code' => 1,  'msg' => '登陆成功']);
            }else{
                return json(['code' => 0,  'msg' => '登陆失败']);
            }
        }else{
            return $this->fetch();
        }

    }

    //手机验证码登陆
    public function login_phone()
    {
        if(request()->isPost()){
            $data['mobile'] = input('post.mobile');

            $userLogin = new LoginModel();
            $info = $userLogin->vailateUserInfo($data);

            if($info){
                session('id', $info['id']);         //用户ID
                session('username', $info['username']);  //用户名
                session('pic', $info['pic']); //用户头像
                session('code', $info['code']);    //推广码
                session('mobile', $info['mobile']);        //手机号

                return json(['code' => 1,  'msg' => '登陆成功']);
            }else{
                return json(['code' => 0,  'msg' => '登陆失败']);
            }
        }else{
            return $this->fetch();
        }
    }

    public function sendLoginCode()
    {
        $data['login_code'] = str_pad(mt_rand(0, 999999), 6, "0", STR_PAD_BOTH);
        $content = '【同旅同途】' . $data['login_code'] . '为您的验证码，请在5分钟内完成登陆。如非本人操作，请忽略。';
        $mobile = input('post.mobile');
//        $code = new SendMessageModel();
//        $info = $code->sendMSM($mobile, $content);
        if ($data) {
            return json(['code' => 1, 'data' => $data, 'msg' => '返回成功']);
        } else {
            return json_encode('获取失败');
        }
    }

    //退出
    public function loginOut()
    {
        Session::clear();
        return $this->fetch('/login/login');
    }
}